home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / ubiqx / ubi_AVLtree.h.z / ubi_AVLtree.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  15.8 KB  |  337 lines

  1. #ifndef ubi_AVLtree_H
  2. #define ubi_AVLtree_H
  3. /* ========================================================================== **
  4.  *                              ubi_AVLtree.h
  5.  *
  6.  *  Copyright (C) 1991-1997 by Christopher R. Hertel
  7.  *
  8.  *  Email: crh@ubiqx.mn.org
  9.  * -------------------------------------------------------------------------- **
  10.  *
  11.  *  This module provides an implementation of AVL height balanced binary
  12.  *  trees.  (Adelson-Velskii, Landis 1962)
  13.  *
  14.  *  This header file contains the basic AVL structure and pointer typedefs
  15.  *  as well as the prototypes needed to access the functions in the AVL
  16.  *  module ubi_AVLtree.  The .c file implements the low-level height balancing
  17.  *  routines that manage the AVL tree, plus all of the basic primops for
  18.  *  adding, searching for, and deleting nodes.
  19.  *
  20.  * -------------------------------------------------------------------------- **
  21.  *
  22.  *  This library is free software; you can redistribute it and/or
  23.  *  modify it under the terms of the GNU Library General Public
  24.  *  License as published by the Free Software Foundation; either
  25.  *  version 2 of the License, or (at your option) any later version.
  26.  *
  27.  *  This library is distributed in the hope that it will be useful,
  28.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  30.  *  Library General Public License for more details.
  31.  *
  32.  *  You should have received a copy of the GNU Library General Public
  33.  *  License along with this library; if not, write to the Free
  34.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  * -------------------------------------------------------------------------- **
  37.  * Log: ubi_AVLtree.h,v
  38.  * Revision 2.5  1997/12/23 04:00:15  crh
  39.  * In this version, all constants & macros defined in the header file have
  40.  * the ubi_tr prefix.  Also cleaned up anything that gcc complained about
  41.  * when run with '-pedantic -fsyntax-only -Wall'.
  42.  *
  43.  * Revision 2.4  1997/07/26 04:36:23  crh
  44.  * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
  45.  * on backwards with respect to node deletion.  I did some more digging and
  46.  * discovered that I was not changing the balance values correctly in the
  47.  * single rotation functions.  Double rotation was working correctly because
  48.  * the formula for changing the balance values is the same for insertion or
  49.  * deletion.  Not so for single rotation.
  50.  *
  51.  * I have tested the fix by loading the tree with over 44 thousand names,
  52.  * deleting 2,629 of them (all those in which the second character is 'u')
  53.  * and then walking the tree recursively to verify that the balance factor of
  54.  * each node is correct.  Passed.
  55.  *
  56.  * Thanks Andrew!
  57.  *
  58.  * Also:
  59.  * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
  60.  * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
  61.  *   hoped they would do (see the bottom of the header file).  They work now.
  62.  *
  63.  * Revision 2.3  1997/06/03 05:22:07  crh
  64.  * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
  65.  * problems.
  66.  *
  67.  * Revision 2.2  1995/10/03 22:15:47  CRH
  68.  * Ubisized!
  69.  *
  70.  * Revision 2.1  95/03/09  23:46:44  CRH
  71.  * Added the ModuleID static string and function.  These modules are now
  72.  * self-identifying.
  73.  * 
  74.  * Revision 2.0  95/03/05  14:11:22  CRH
  75.  * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
  76.  * and so includes all of the changes to that module.  In addition, a bug in
  77.  * the node deletion process has been fixed.
  78.  *
  79.  * After rewriting the Locate() function in ubi_BinTree, I decided that it was
  80.  * time to overhaul this module.  In the process, I discovered a bug related
  81.  * to node deletion.  To fix the bug, I wrote function Debalance().  A quick
  82.  * glance will show that it is very similar to the Rebalance() function.  In
  83.  * previous versions of this module, I tried to include the functionality of
  84.  * Debalance() within Rebalance(), with poor results.
  85.  *
  86.  * Revision 1.0  93/10/15  22:58:48  CRH
  87.  * With this revision, I have added a set of #define's that provide a single,
  88.  * standard API to all existing tree modules.  Until now, each of the three
  89.  * existing modules had a different function and typedef prefix, as follows:
  90.  *
  91.  *       Module        Prefix
  92.  *     ubi_BinTree     ubi_bt
  93.  *     ubi_AVLtree     ubi_avl
  94.  *     ubi_SplayTree   ubi_spt
  95.  *
  96.  * To further complicate matters, only those portions of the base module
  97.  * (ubi_BinTree) that were superceeded in the new module had the new names.
  98.  * For example, if you were using ubi_AVLtree, the AVL node structure was
  99.  * named "ubi_avlNode", but the root structure was still "ubi_btRoot".  Using
  100.  * SplayTree, the locate function was called "ubi_sptLocate", but the next
  101.  * and previous functions remained "ubi_btNext" and "ubi_btPrev".
  102.  *
  103.  * This was not too terrible if you were familiar with the modules and knew
  104.  * exactly which tree model you wanted to use.  If you wanted to be able to
  105.  * change modules (for speed comparisons, etc), things could get messy very
  106.  * quickly.
  107.  *
  108.  * So, I have added a set of defined names that get redefined in any of the
  109.  * descendant modules.  To use this standardized interface in your code,
  110.  * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
  111.  * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or
  112.  * datatype names for the module that you are using.  Just remember to
  113.  * include the header for that module in your program file.  Because these
  114.  * names are handled by the preprocessor, there is no added run-time
  115.  * overhead.
  116.  *
  117.  * Note that the original names do still exist, and can be used if you wish
  118.  * to write code directly to a specific module.  This should probably only be
  119.  * done if you are planning to implement a new descendant type, such as
  120.  * red/black trees.  CRH
  121.  *
  122.  *  V0.0 - May, 1990   -  Written by Christopher R. Hertel (CRH).
  123.  *
  124.  *  ========================================================================= **
  125.  */
  126.  
  127. #include "ubi_BinTree.h"   /* Base erg binary tree support.       */
  128.  
  129. /*  ------------------------------------------------------------------------- **
  130.  *  AVL Tree Node Structure:  This structure defines the basic elements of
  131.  *       the AVL tree nodes.  In general you *SHOULD NOT PLAY WITH THESE
  132.  *       FIELDS*!  But, of course, I have to put the structure into this
  133.  *       header so that you can use the structure as a building block.
  134.  *
  135.  *  The fields are as follows:
  136.  *    Link     -  An array of pointers.  These pointers are manipulated by the
  137.  *                BT and AVL routines, and indicate the left and right child
  138.  *                nodes, plus the parent node.  By keeping track of the parent
  139.  *                pointer, we avoid the need for recursive routines or hand-
  140.  *                tooled stacks to keep track of our path back to the root.
  141.  *                The use of these pointers is subject to change without
  142.  *                notice.
  143.  *    gender   -  For tree rebalancing purposes, it is necessary that each node
  144.  *                know whether it is the left or right child of its parent, or
  145.  *                if it is the root.  This information is stored in this field.
  146.  *    balance  -  This field is also needed for AVL balancing purposes.  It
  147.  *                indicates which subtree of the current node is longer, or if
  148.  *                the subtrees are, in fact, balanced with respect to each
  149.  *                other.
  150.  *  ------------------------------------------------------------------------- **
  151.  */
  152.  
  153. typedef struct ubi_avlNodeStruct {
  154.   struct ubi_avlNodeStruct
  155.          *Link[3];      /* Normal Binary Tree Node type.                      */
  156.   char    gender;       /* The node is either the RIGHT or LEFT child of its  */
  157.                         /* parent, or is the root node.                       */
  158.   char    balance;      /* In an AVL tree, each node is the root of a subtree */
  159.                         /* that may be balanced, or be one node longer to the */
  160.                         /* right or left.  This field keeps track of the      */
  161.                         /* balance value of each node.                        */
  162.     } ubi_avlNode;
  163.  
  164. typedef ubi_avlNode *ubi_avlNodePtr;    /* a Pointer to an AVL node */
  165.  
  166. /* -------------------------------------------------------------------------- **
  167.  *  Function prototypes.
  168.  * -------------------------------------------------------------------------- **
  169.  */
  170.  
  171. ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr );
  172.   /* ------------------------------------------------------------------------ **
  173.    * Initialize a tree node.
  174.    *
  175.    *  Input:   NodePtr  - a pointer to a ubi_btNode structure to be
  176.    *                      initialized.
  177.    *  Output:  a pointer to the initialized ubi_avlNode structure (ie. the
  178.    *           same as the input pointer).
  179.    * ------------------------------------------------------------------------ **
  180.    */
  181.  
  182. ubi_trBool ubi_avlInsert( ubi_btRootPtr   RootPtr,
  183.                           ubi_avlNodePtr  NewNode,
  184.                           ubi_btItemPtr   ItemPtr,
  185.                           ubi_avlNodePtr *OldNode );
  186.   /* ------------------------------------------------------------------------ **
  187.    * This function uses a non-recursive algorithm to add a new element to
  188.    * the tree.
  189.    *
  190.    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
  191.    *                       the root of the tree to which NewNode is to be added.
  192.    *           NewNode  -  a pointer to an ubi_avlNode structure that is NOT
  193.    *                       part of any tree.
  194.    *           ItemPtr  -  A pointer to the sort key that is stored within
  195.    *                       *NewNode.  ItemPtr MUST point to information stored
  196.    *                       in *NewNode or an EXACT DUPLICATE.  The key data
  197.    *                       indicated by ItemPtr is used to place the new node
  198.    *                       into the tree.
  199.    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
  200.    *                       the tree, a duplicate node may be found.  If
  201.    *                       duplicates are allowed, then the new node will
  202.    *                       be simply placed into the tree.  If duplicates
  203.    *                       are not allowed, however, then one of two things
  204.    *                       may happen.
  205.    *                       1) if overwritting *is not* allowed, this
  206.    *                          function will return FALSE (indicating that
  207.    *                          the new node could not be inserted), and
  208.    *                          *OldNode will point to the duplicate that is
  209.    *                          still in the tree.
  210.    *                       2) if overwritting *is* allowed, then this
  211.    *                          function will swap **OldNode for *NewNode.
  212.    *                          In this case, *OldNode will point to the node
  213.    *                          that was removed (thus allowing you to free
  214.    *                          the node).
  215.    *                          **  If you are using overwrite mode, ALWAYS  **
  216.    *                          ** check the return value of this parameter! **
  217.    *                 Note: You may pass NULL in this parameter, the
  218.    *                       function knows how to cope.  If you do this,
  219.    *                       however, there will be no way to return a
  220.    *                       pointer to an old (ie. replaced) node (which is
  221.    *                       a problem if you are using overwrite mode).
  222.    *
  223.    *  Output:  a boolean value indicating success or failure.  The function
  224.    *           will return FALSE if the node could not be added to the tree.
  225.    *           Such failure will only occur if duplicates are not allowed,
  226.    *           nodes cannot be overwritten, AND a duplicate key was found
  227.    *           within the tree.
  228.    * ------------------------------------------------------------------------ **
  229.    */
  230.  
  231. ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr  RootPtr,
  232.                               ubi_avlNodePtr DeadNode );
  233.   /* ------------------------------------------------------------------------ **
  234.    * This function removes the indicated node from the tree, after which the
  235.    * tree is rebalanced.
  236.    *
  237.    *  Input:  RootPtr  -  A pointer to the header of the tree that contains
  238.    *                      the node to be removed.
  239.    *          DeadNode -  A pointer to the node that will be removed.
  240.    *
  241.    *  Output: This function returns a pointer to the node that was removed
  242.    *          from the tree (ie. the same as DeadNode).
  243.    *
  244.    *  Note:   The node MUST be in the tree indicated by RootPtr.  If not,
  245.    *          strange and evil things will happen to your trees.
  246.    * ------------------------------------------------------------------------ **
  247.    */
  248.  
  249. int ubi_avlModuleID( int size, char *list[] );
  250.   /* ------------------------------------------------------------------------ **
  251.    * Returns a set of strings that identify the module.
  252.    *
  253.    *  Input:  size  - The number of elements in the array <list>.
  254.    *          list  - An array of pointers of type (char *).  This array
  255.    *                  should, initially, be empty.  This function will fill
  256.    *                  in the array with pointers to strings.
  257.    *  Output: The number of elements of <list> that were used.  If this value
  258.    *          is less than <size>, the values of the remaining elements are
  259.    *          not guaranteed.
  260.    *
  261.    *  Notes:  Please keep in mind that the pointers returned indicate strings
  262.    *          stored in static memory.  Don't free() them, don't write over
  263.    *          them, etc.  Just read them.
  264.    * ------------------------------------------------------------------------ **
  265.    */
  266.  
  267. /* -------------------------------------------------------------------------- **
  268.  * Masquarade...
  269.  *
  270.  * This set of defines allows you to write programs that will use any of the
  271.  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
  272.  * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
  273.  * type by including the appropriate module header.
  274.  */
  275.  
  276. #undef ubi_trNode
  277. #undef ubi_trNodePtr
  278. #define ubi_trNode    ubi_avlNode
  279. #define ubi_trNodePtr ubi_avlNodePtr
  280.  
  281. #undef ubi_trInitNode
  282. #define ubi_trInitNode( Np ) ubi_avlInitNode( (ubi_avlNodePtr)(Np) )
  283.  
  284. #undef ubi_trInsert
  285. #define ubi_trInsert( Rp, Nn, Ip, On ) \
  286.         ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Nn), \
  287.                        (ubi_btItemPtr)(Ip), (ubi_avlNodePtr *)(On) )
  288.  
  289. #undef ubi_trRemove
  290. #define ubi_trRemove( Rp, Dn ) \
  291.         ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Dn) )
  292.  
  293. #undef ubi_trLocate
  294. #define ubi_trLocate( Rp, Ip, Op ) \
  295.         (ubi_avlNodePtr)ubi_btLocate( (ubi_btRootPtr)(Rp), \
  296.                                       (ubi_btItemPtr)(Ip), \
  297.                                       (ubi_trCompOps)(Op) )
  298.  
  299. #undef ubi_trFind
  300. #define ubi_trFind( Rp, Ip ) \
  301.         (ubi_avlNodePtr)ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
  302.  
  303. #undef ubi_trNext
  304. #define ubi_trNext( P ) (ubi_avlNodePtr)ubi_btNext( (ubi_btNodePtr)(P) )
  305.  
  306. #undef ubi_trPrev
  307. #define ubi_trPrev( P ) (ubi_avlNodePtr)ubi_btPrev( (ubi_btNodePtr)(P) )
  308.  
  309. #undef ubi_trFirst
  310. #define ubi_trFirst( P ) (ubi_avlNodePtr)ubi_btFirst( (ubi_btNodePtr)(P) )
  311.  
  312. #undef ubi_trLast
  313. #define ubi_trLast( P ) (ubi_avlNodePtr)ubi_btLast( (ubi_btNodePtr)(P) )
  314.  
  315. #undef ubi_trFirstOf
  316. #define ubi_trFirstOf( Rp, Ip, P ) \
  317.         (ubi_avlNodePtr)ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
  318.                        (ubi_btItemPtr)(Ip), \
  319.                        (ubi_btNodePtr)(P) )
  320.  
  321. #undef ubi_trLastOf
  322. #define ubi_trLastOf( Rp, Ip, P ) \
  323.         (ubi_avlNodePtr)ubi_btLastOf( (ubi_btRootPtr)(Rp), \
  324.                                       (ubi_btItemPtr)(Ip), \
  325.                                       (ubi_btNodePtr)(P) )
  326.  
  327. #undef ubi_trLeafNode
  328. #define ubi_trLeafNode( Nd ) \
  329.         (ubi_avlNodePtr)ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
  330.  
  331. #undef ubi_trModuleID
  332. #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
  333.  
  334.  
  335. /* =========================== End  ubi_AVLtree.h =========================== */
  336. #endif /* ubi_AVLtree_H */
  337.